//
//  MainAnuseViewController.m
//  Funny
//
//  Created by dllo on 15/11/2.
//  Copyright © 2015年 King. All rights reserved.
//

#import "MainAnuseViewController.h"
#import "FristTableViewCell.h"

@interface MainAnuseViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) UIImageView *pic;
@property (nonatomic, strong) MainAnuseModel *model;
@property (nonatomic, assign) BOOL isOpen;

@end

@implementation MainAnuseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isOpen = YES;
    [self handleData];
    [self handleDataForUserInfo];
    [self.view addSubview:self.tableView];
    [self.tableView addSubview:self.pic];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"share.png"] style:UIBarButtonItemStyleDone target:self action:@selector(shareButtonAction:)];
}

// 分享按钮
- (void)shareButtonAction:(id)sender
{
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    [imageView sd_setImageWithURL:_model.pics[0]];
    NSString *str = [NSString string];
    if ([[_model.baseinfo objectForKey:@"brief"] isEqualToString:@""]) {
        str = @"暂无评论";
    }
    else{
        str = [_model.baseinfo objectForKey:@"brief"];
    }
    [shareParams SSDKSetupShareParamsByText:[NSString stringWithFormat:@"#分享来自YYY的分享#  %@",str]
                                     images:imageView.image
                                        url:nil
                                      title:@"分享标题"
                                       type:SSDKContentTypeAuto];
    
    [ShareSDK showShareActionSheet:sender
                             items:nil
                       shareParams:shareParams
               onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
                   switch (state) {
                       case SSDKResponseStateBegin:
                       {
                           NSLog(@"开始分享");
                       }
                           break;
                       case SSDKResponseStateSuccess:
                       {
                           
                           UIAlertController *alter = [UIAlertController alertControllerWithTitle:@"分享成功" message:nil preferredStyle:1];
                           UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                               ;
                           }];
                           [alter addAction:okAction];
                           [self presentViewController:alter animated:YES completion:^{
                               
                           }];
                       }
                           break;
                       case SSDKResponseStateFail:
                       {
                           UIAlertController *alter = [UIAlertController alertControllerWithTitle:@"分享失败" message:nil preferredStyle:1];
                           UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                               ;
                           }];
                           [alter addAction:okAction];
                           [self presentViewController:alter animated:YES completion:^{
                               
                           }];
                       }
                           break;
                       case SSDKResponseStateCancel:
                       {
                           
                           UIAlertController *alter = [UIAlertController alertControllerWithTitle:@"分享取消" message:nil preferredStyle:1];
                           UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                               ;
                           }];
                           [alter addAction:okAction];
                           [self presentViewController:alter animated:YES completion:^{
                               
                           }];
                       }
                           break;
                       default:
                           break;
                   }
               }];
}

- (UIImageView *)pic
{
    if (!_pic) {
        self.pic = [[UIImageView alloc]initWithFrame:CGRectMake(0, -ScreenWidth / 3 * 2, ScreenWidth, ScreenWidth / 3 * 2)];
    }
    return _pic;
}

// 存放数据
- (NSMutableArray *)dataArr
{
    if (!_dataArr) {
        self.dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

// tableView懒加载
- (UITableView *)tableView
{
    if (!_tableView) {
        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 20) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.contentInset = UIEdgeInsetsMake(ScreenWidth / 3 * 2, 0, 0, 0);
        _tableView.showsVerticalScrollIndicator = NO;
        [_tableView registerClass:[MainAnuseTableViewCell class] forCellReuseIdentifier:@"reuse"];
        [_tableView registerClass:[FristTableViewCell class] forCellReuseIdentifier:@"use"];
    }
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 320;
    }
    else{
        return 100;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        FristTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"use"];
        cell.model = self.model;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.arr = self.model.pics;
        return cell;
    }else{
        MainAnuseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
        cell.model = _dataArr[indexPath.row - 1];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}

// 数据处理 网络请求 tableView
- (void)handleDataForUserInfo
{
    NSString *url = [NSString stringWithFormat:@"http://api.mxtrip.cn/api/ugc/getPlaceTagsComment?placeid=%@&num=3&tagtype=3&from=1&_tk=1446516517:12448&apiVersion=1.1&clientVersion=1.2.0&idfa=A857AE53-A1D1-4331-9E4E-62937AB3C688",self.ID];
    // 网络请求封装
    [NetHandler getDataWithURL:url completion:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *dataDic = [dic objectForKey:@"data"];
        for (NSDictionary *modelDic in [dataDic objectForKey:@"comment"]) {
            MainCommentModel *model = [MainCommentModel modelWithDic:modelDic];
            [self.dataArr addObject:model];
        }
        [self.tableView reloadData];
    }];
}

// 介绍 数据请求
- (void)handleData
{
    NSString *url = [NSString stringWithFormat:@"http://api.mxtrip.cn/api/place/getPlace?placeid=%@",self.ID];
    [NetHandler getDataWithURL:url completion:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        self.model = [MainAnuseModel modelWithDic:[dic objectForKey:@"data"]];
        [self.pic sd_setImageWithURL:[NSURL URLWithString:_model.pics[0]] placeholderImage:[UIImage imageNamed:@"chartlet_image_traning@2x.png"]];
        [self.tableView reloadData];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
